/**
 * Created by gongxun on 17/4/20.
 */

var KwxRoomDaikaiLayer = KwxSecondBase.extend({
    Image_yikai : null,
    Image_daikai : null,

    Panel_yikai : null,
    Panel_daikai : null,

    ListView_yikai : null,
    ListView_daikai : null,

    mYikaiTables : null,
    mDaikaiTables : null,

    mIndex : null,
    mDelaytime : null,
    ctor: function () {
        this._super();
        this.loadSecondCCS("res/ccs/kwxRoomDaikai.json");

        //修改
        if (mo.gameHelper.projectCode != 2){
            mo.seekWidgetByName(this.ccsNode, "Image_title_bg").setVisible(false);
        }else{

        }

        //ui
        this.Panel_yikai = mo.seekWidgetByName(this.ccsNode, "Panel_yikai");
        this.ListView_yikai = mo.seekWidgetByName(this.ccsNode, "ListView_yikai");

        this.Panel_daikai = mo.seekWidgetByName(this.ccsNode, "Panel_daikai");
        this.ListView_daikai = mo.seekWidgetByName(this.ccsNode, "ListView_daikai");

        this.Image_yikai = mo.seekWidgetByName(this.ccsNode, "Image_yikai");
        this.Image_daikai = mo.seekWidgetByName(this.ccsNode, "Image_daikai");

        //btn
        mo.seekWidgetByName(this.ccsNode, "Panel_touch_daikai").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)){
                this.intoIndex(1);
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Panel_touch_yikai").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)){
                this.intoIndex(0);
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_refish").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEnd(type)){
                sender.setEnabled(false);
                sender.runAction(cc.sequence(cc.delayTime(3), cc.callFunc(function(){
                    sender.setEnabled(true);
                })));

                if (this.mIndex == 0){
                    //获取已开记录
                    mo.gameMsgManager.sendOpenTable();
                }else{
                    mo.gameMsgManager.sendOpenTableHistory();
                }
            }
        }, this);

        this.mDelaytime = mo.gameHelper.getMillisecond();
        this.intoIndex(0);
    },

    onEnter: function () {
        this._super();

        this.loadItemTemple();

        mo.eventHelper.add("TableInfoChanges", this, function(tables){
            this.updateYikaiItems(tables);
        });
        mo.gameMsgManager.sendOpenTable();

        mo.eventHelper.add("TableInfoHistoryChanges", this, function(tables){
            this.updateDaikaiItems(tables);
        });
        mo.gameMsgManager.sendOpenTableHistory();

        this.schedule(function(dt){
            //5秒刷新
            mo.gameMsgManager.sendOpenTable();
            mo.gameMsgManager.sendOpenTableHistory();
        }, 5.0);
    },

    onExit: function () {
        mo.eventHelper.remove(this);
        this._super();
    },

    intoIndex : function(index){
        if (index == this.mIndex){
            return;
        }

        this.mIndex = index;
        //超过3秒切换 就去服务器拉
        if (mo.gameHelper.getMillisecond() - this.mDelaytime > 3000){
            this.mDelaytime = mo.gameHelper.getMillisecond();

            if (this.mIndex == 0){
                //获取已开记录
                mo.gameMsgManager.sendOpenTable();
            }else{
                mo.gameMsgManager.sendOpenTableHistory();
            }
        }

        this.Image_yikai.setVisible(index == 0);
        this.Image_daikai.setVisible(index == 1);

        this.Panel_yikai.setVisible(index == 0);
        this.Panel_daikai.setVisible(index == 1);
    },

    loadItemTemple : function(){
        var temple = mo.seekWidgetByName(this.Panel_yikai, "Panel_temple");
        this.ListView_yikai.setItemModel(temple);
        this.ListView_yikai.removeAllChildren();

        var temple2 = mo.seekWidgetByName(this.ListView_daikai, "Panel_temple");
        this.ListView_daikai.setItemModel(temple2);
        this.ListView_daikai.removeAllChildren();
    },

    addYikaiItem : function(){
        this.ListView_yikai.pushBackDefaultItem();
        var ui = this.ListView_yikai.getItem(this.ListView_yikai.getItems().length - 1);
        var height = mo.gameHelper.projectCode == 2 ? 160 : 140;
        var size = cc.size(this.ListView_yikai.getContentSize().width, height);

        ui.setContentSize(size);
        mo.seekWidgetByName(ui, "Image_bg").setContentSize(cc.size(size.width, height));
        var offsetBtnX = mo.gameHelper.projectCode == 2 ? 85 : 62;
        mo.seekWidgetByName(ui, "Button_jiesan").setPositionX(size.width - offsetBtnX);
        mo.seekWidgetByName(ui, "Button_yaoqing").setPositionX(size.width - offsetBtnX);

        return ui;
    },

    updateYikaiItems : function(tables){
        this.mYikaiTables = tables;

        //保持ui一致
        while (this.ListView_yikai.getItems().length > tables.length){
            this.ListView_yikai.removeLastItem();
        }
        while (this.ListView_yikai.getItems().length < tables.length){
            this.addYikaiItem();
        }

        //重新赋值
        for (var i = 0; i < tables.length; i++){
            var tableInfo = tables[i];
            var ui = this.ListView_yikai.getItems()[i];

            var gameName = mo.gameHelper.getGameNameForRoomId(tableInfo.TableId);
            var config = JSON.parse(tableInfo.CfgJson);

            //Text_roomId
            var Text_roomId = mo.seekWidgetByName(ui, "Text_roomId");
            mo.gameHelper.setTextString(Text_roomId, tableInfo.TableId + "");

            //Image_status
            var statusText = "res/ccs/hall_second/daikai/text_weikaishi.png";
            if (tableInfo.PlayTimes != 0){
                statusText = "res/ccs/hall_second/daikai/text_jinxingzhong.png";
            }
            var Image_status = mo.seekWidgetByName(ui, "Image_status");
            mo.gameHelper.loadTextureImage(Image_status, statusText);

            //Text_wanfa
            var Text_wanfa = mo.seekWidgetByName(ui, "Text_wanfa");
            var Text_wanfaText = mo.gameHelper.getRuleConfigSummary(config, gameName, parseInt(tableInfo.TableId));
            mo.gameHelper.setTextString(Text_wanfa, mo.gameHelper.getCHSNameForGameName(gameName) + "规则：" + Text_wanfaText);

            //tiem
            var date = new Date(tableInfo.OpenTime * 1000);
            var year = date.getFullYear();
            var month = mo.gameHelper.getFullString((date.getMonth() + 1), "0", 2);
            var day = mo.gameHelper.getFullString(date.getDate(), "0", 2);
            var hours = mo.gameHelper.getFullString(date.getHours(), "0", 2);
            var min = mo.gameHelper.getFullString(date.getMinutes(), "0", 2);
            var sec = mo.gameHelper.getFullString(date.getSeconds(), "0", 2);

            //time1
            var Text_time1 = mo.seekWidgetByName(ui, "Text_time1");
            mo.gameHelper.setTextString(Text_time1, year + "-" + month + "-" + day);

            //time2
            var Text_time2 = mo.seekWidgetByName(ui, "Text_time2");
            mo.gameHelper.setTextString(Text_time2, hours + ":" + min + ":" + sec);

            //player
            var userCount = tableInfo.users.length;
            var addUserX = 0;
            if (userCount > 1){
                //addUserX = ((240 + mo.curSize.x - 960) / (userCount - 1));
                addUserX = (240 / (userCount - 1));
            }
            for (var j = 0; j < 5; j++){
                var player = mo.seekWidgetByName(ui, "Panel_player" + j);
                if (j < userCount){
                    player.setVisible(true);
                    player.setPositionX(j * addUserX);

                    var playerInfo = tableInfo.users[j];

                    //nick
                    var Text_nick = mo.seekWidgetByName(player, "Text_nick");
                    mo.gameHelper.setTextString(Text_nick, mo.gameHelper.getUserNickFilterForCount(playerInfo.UserBase.nickname, 3));

                    //head
                    mo.gameHelper.loadImageForUrl(mo.seekWidgetByName(player, "Image_head"), playerInfo.UserBase.faceUrl);

                    //score
                    var Text_score = mo.seekWidgetByName(player, "Text_score");
                    mo.gameHelper.setTextString(Text_score, playerInfo.Score);
                    if (playerInfo.Score >= 0){
                        Text_score.setTextColor(cc.color(0, 128, 0));
                    }else{
                        Text_score.setTextColor(cc.color(165, 42, 42));
                    }

                }else{
                    player.setVisible(false);
                }
            }

            //邀请
            var Button_yaoqing = mo.seekWidgetByName(ui, "Button_yaoqing");
            Button_yaoqing.setTag(i);
            Button_yaoqing.addTouchEventListener(function(sender, type){
                if (mo.gameHelper.getIsTouchEnd(type)){
                    this.onYaoqing(sender.getTag());
                }
            }, this);

            //解散
            var Button_jiesan = mo.seekWidgetByName(ui, "Button_jiesan");
            Button_jiesan.setTag(i);
            Button_jiesan.addTouchEventListener(function(sender, type){
                if (mo.gameHelper.getIsTouchEnd(type)){
                    this.onJiesan(sender.getTag());
                }
            }, this);

            //隐藏按钮
            if (tableInfo.PlayTimes != 0){
                Button_yaoqing.setVisible(false);
                Button_jiesan.setVisible(false);
            }else{
                Button_yaoqing.setVisible(true);
                Button_jiesan.setVisible(true);
            }
        }
    },

    addDaikaiItem : function(){
        this.ListView_daikai.pushBackDefaultItem();
        var ui = this.ListView_daikai.getItem(this.ListView_daikai.getItems().length - 1);
        var height = mo.gameHelper.projectCode == 2 ? 160 : 140;
        var size = cc.size(this.ListView_daikai.getContentSize().width, height);

        ui.setContentSize(size);
        mo.seekWidgetByName(ui, "Image_bg").setContentSize(cc.size(size.width, height));

        return ui;
    },

    updateDaikaiItems : function(tables){
        this.mDaikaiTables = tables;

        //保持ui一致
        while (this.ListView_daikai.getItems().length > tables.length){
            this.ListView_daikai.removeLastItem();
        }
        while (this.ListView_daikai.getItems().length < tables.length){
            this.addDaikaiItem();
        }

        //重新赋值
        for (var i = 0; i < tables.length; i++){
            var tableInfo = tables[i];
            var ui = this.ListView_daikai.getItems()[i];

            var gameName = mo.gameHelper.getGameNameForRoomId(tableInfo.TableId);
            var config = JSON.parse(tableInfo.CfgJson);

            //Text_roomId
            var Text_roomId = mo.seekWidgetByName(ui, "Text_roomId");
            mo.gameHelper.setTextString(Text_roomId, tableInfo.TableId + "");

            //Text_gameStats
            // var Text_gameStats = mo.seekWidgetByName(ui, "Text_gameStats");
            // mo.gameHelper.setTextString(Text_gameStats, "已结束");

            //Text_wanfa
            var Text_wanfa = mo.seekWidgetByName(ui, "Text_wanfa");
            var Text_wanfaText = mo.gameHelper.getRuleConfigSummary(config, gameName, parseInt(tableInfo.TableId));
            mo.gameHelper.setTextString(Text_wanfa, mo.gameHelper.getCHSNameForGameName(gameName) + "规则：" + Text_wanfaText);

            //tiem
            var date = new Date(tableInfo.OpenTime * 1000);
            var year = date.getFullYear();
            var month = mo.gameHelper.getFullString((date.getMonth() + 1), "0", 2);
            var day = mo.gameHelper.getFullString(date.getDate(), "0", 2);
            var hours = mo.gameHelper.getFullString(date.getHours(), "0", 2);
            var min = mo.gameHelper.getFullString(date.getMinutes(), "0", 2);
            var sec = mo.gameHelper.getFullString(date.getSeconds(), "0", 2);

            //time1
            var Text_time1 = mo.seekWidgetByName(ui, "Text_time1");
            mo.gameHelper.setTextString(Text_time1, year + "-" + month + "-" + day);

            //time2
            var Text_time2 = mo.seekWidgetByName(ui, "Text_time2");
            mo.gameHelper.setTextString(Text_time2, hours + ":" + min + ":" + sec);

            //player
            var userCount = tableInfo.users.length;
            var addUserX = 0;
            if (userCount > 1){
                //addUserX = ((240 + mo.curSize.x - 960) / (userCount - 1));
                addUserX = (240 / (userCount - 1));
            }
            for (var j = 0; j < 5; j++){
                var player = mo.seekWidgetByName(ui, "Panel_player" + j);
                if (j < userCount){
                    player.setVisible(true);
                    player.setPositionX(j * addUserX);

                    var playerInfo = tableInfo.users[j];

                    //nick
                    var Text_nick = mo.seekWidgetByName(player, "Text_nick");
                    mo.gameHelper.setTextString(Text_nick, mo.gameHelper.getUserNickFilterForCount(playerInfo.UserBase.nickname, 3));

                    //head
                    mo.gameHelper.loadImageForUrl(mo.seekWidgetByName(player, "Image_head"), playerInfo.UserBase.faceUrl);

                    //score
                    var Text_score = mo.seekWidgetByName(player, "Text_score");
                    mo.gameHelper.setTextString(Text_score, playerInfo.Score);
                    if (playerInfo.Score >= 0){
                        Text_score.setTextColor(cc.color(0, 128, 0));
                    }else{
                        Text_score.setTextColor(cc.color(165, 42, 42));
                    }

                }else{
                    player.setVisible(false);
                }
            }
        }
    },

    onYaoqing : function(index){
        if (index >= this.mYikaiTables.length){
            return;
        }

        var tableInfo = this.mYikaiTables[index];
        var tableId = tableInfo.TableId;
        var config = JSON.parse(tableInfo.CfgJson);
        var gameName = mo.gameHelper.getGameNameForRoomId(tableId);
        var gameCHSName = mo.gameHelper.getCHSNameForGameName(gameName);
        var configSummary = mo.gameHelper.getRuleConfigSummary(config, gameName, parseInt(tableId));
        configSummary += "，就等你来了(点击加入)";

        var title = "桌号:[" + tableId + "]" + "-" + mo.gameHelper.projectName + "-" + gameCHSName;
        var description = configSummary;
        mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(1, tableId));
    },

    onJiesan : function(index){
        if (index >= this.mYikaiTables.length){
            return;
        }

        var tableInfo = this.mYikaiTables[index];
        var tableId = tableInfo.TableId;
        mo.showMsgDialogOkAndCancel("是确定要解散该房间？", function(){
            if (tableId == this.mYikaiTables[index].TableId){
                mo.gameMsgManager.sendCancleOpenTable(tableInfo.TableId);
            }
        }, this, function(){

        }, this);
    }
});